import proxyMiddleware from 'http-proxy-middleware'
import k2c from 'koa2-connect'

import config from '../../config'
const proxyTable = process.env.NODE_ENV === 'production' ? config.pro.proxyTable : config.dev.proxyTable
export const asset = app => {
  // 代理请求
  Object.keys(proxyTable).forEach(context => {
    var options = proxyTable[context]
    if (typeof options === 'string') {
      options = { target: options }
    }
    app.use(k2c(proxyMiddleware(options.filter || context, options)))
  })
}
